import $api from "@/api/system.js";

const TRACK_TYPE = {
  GOOD_DETAIL: "GoodsDetailPage",
  BUY_NOW: "BuyNow",
  ADD_CART: "AddCart",
  ASK: "ConsultMerchants",
  CLICK_PHONE: "ConsultMerchantsClickPhone",
  KNOW: "ConsultMerchantsClickKnow",
  CHANNEL_REGISTER: "MemberChannelRegister",
  GET_STORE_PHONE_COUNT: "ContactPhoneClick",
  AdjustPriceNotifyClick: "AdjustPriceNotifyClick",
  ArrivalNoticeClick: "ArrivalNoticeClick",
  StoreSearchClick: "StoreSearchClick",
  ACTIVITY_PAGE_CLICK: "ActivityPageClick",
  ACTIVITY_PAGE_REGISTER_CLICK: "ActivityPageRegisterClick",
  ACTIVITY_PAGE_LICENCE_APPLY_CLICK: "ActivityPageLicenceApplyClick",
  ACTIVITY_PAGE_BUY_NOW_CLICK: "ActivityPageBuyNowClick",
  REGISTER_ACTIVITY_POPUP_CLICK: "RegisterActivityPopupClick",
  CLOSE_REGISTER_ACTIVITY_POPUP_CLICK: "CloseRegisterActivityPopupClick",
  CLOSE_ACTIVITY_PAGE_LICENCE_APPLY_CLICK: "CloseActivityPageLicenceApplyClick",
  CLOCKED_ACTIVITY_PAGE_CLICK: "ClockedActivityBuyerPageClick",
};

const PLATFORM = { PC: 1, WEBAPP: 2 };

const handleParams = list => {
  const param = list.reduce((res, item, index) => {
    res[`param${index + 1}`] = item;
    return res;
  }, {});
  return { param, platform: PLATFORM.WEBAPP };
};
/**
 * 商品详情页
 */
export function trackEventGoodsDetail({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.GOOD_DETAIL, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("商品详情页,有料定义埋点数据发送失败");
  }
}

export function trackEventBuyNow({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.BUY_NOW, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("立即购买,有料定义埋点数据发送失败");
  }
}

export function trackEventAddCart({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ADD_CART, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("加入购物车,有料定义埋点数据发送失败");
  }
}

export function trackEventAsk({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ASK, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("咨询商家,有料定义埋点数据发送失败");
  }
}

export function trackEventClickPhone({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.CLICK_PHONE, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("咨询商家弹框-点击手机号,有料定义埋点数据发送失败");
  }
}
export function trackEventKnow({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.KNOW, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("咨询商家弹框-知道了,有料定义埋点数据发送失败");
  }
}
/* 通过注册渠道注册 */
export function trackChannelRegister({ registerChannelId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.CHANNEL_REGISTER, ...handleParams([registerChannelId]) });
  } catch (e) {
    console.log("注册渠道注册-注册成功,有料定义埋点数据发送失败");
  }
}

// 点击获取手机号次数
export function trackEventGetStorePhoneCount({ storeId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.GET_STORE_PHONE_COUNT, ...handleParams([storeId]) });
  } catch (e) {
    console.log("商品详情/店铺详情-点击弹窗获取手机号,有料定义埋点数据发送失败");
  }
}

// 点击获取调价通知次数
export function AdjustPriceNotifyClick({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.AdjustPriceNotifyClick, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("商品详情/店铺详情-点击调价通知,有料定义埋点数据发送失败");
  }
}

// 点击获取到货通知次数
export function ArrivalNoticeClick({ storeId, goodsId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ArrivalNoticeClick, ...handleParams([storeId, goodsId]) });
  } catch (e) {
    console.log("商品详情/店铺详情-点击到货通知,有料定义埋点数据发送失败");
  }
}

// 店内搜索
export function trackEventStoreSearchClick({ storeId }) {
  try {
    $api.collect({ collectionType: TRACK_TYPE.StoreSearchClick, ...handleParams([storeId]) });
  } catch (e) {
    console.log("店内搜索,有料定义埋点数据发送失败");
  }
}

// 活动期间，加载活动落地页
export function trackEventActivityPageClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ACTIVITY_PAGE_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("活动期间，加载活动落地页");
  }
}
// 活动期间，注册弹窗点注册按钮
export function trackEventActivityPageRegisterClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ACTIVITY_PAGE_REGISTER_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("活动期间，注册弹窗点注册按钮");
  }
}
// 活动期间，企业认证弹窗点击提交按钮
export function trackEventActivityPageLicenceApplyClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ACTIVITY_PAGE_LICENCE_APPLY_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("活动期间，企业认证弹窗点击提交按钮");
  }
}
// 活动期间，点击提交订单按钮
export function trackEventActivityPageBuyNowClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.ACTIVITY_PAGE_BUY_NOW_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("活动期间，点击提交订单按钮");
  }
}
// 活动期间，活动落地页点击活动规则
export function trackEventRegisterActivityPopupClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.REGISTER_ACTIVITY_POPUP_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("活动期间，活动落地页点击活动规则");
  }
}
// 活动期间，活动落地页点击关闭活动规则
export function trackEventCloseRegisterActivityPopupClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.CLOSE_REGISTER_ACTIVITY_POPUP_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log(" 活动期间，活动落地页点击关闭活动规则");
  }
}
// 活动期间，关闭企业认证弹窗
export function trackEventCloseActivityPageLicenceApplyClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.CLOSE_ACTIVITY_PAGE_LICENCE_APPLY_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log(" 活动期间，关闭企业认证弹窗");
  }
}

export function trackEventClockedActivityPageClick() {
  try {
    $api.collect({ collectionType: TRACK_TYPE.CLOCKED_ACTIVITY_PAGE_CLICK, ...handleParams([]) });
  } catch (e) {
    console.log("进入打卡页面");
  }
}
